<template>
  <div class="rermissionPage-container">
    <PageTools>
      <template #after>
        <el-button type="primary" size="small" @click="onAdd({pid: '0', type: 1})">添加权限</el-button>
      </template>
    </PageTools>
    <el-table :data="list" border row-key="id">
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="code" label="标识"></el-table-column>
      <el-table-column prop="description" label="描述"></el-table-column>
      <el-table-column prop="" label="操作">
        <template v-slot="{ row }">
          <el-button type="text" @click="onAdd({pid: row.id,type: row.type +1})">添加</el-button>
          <el-button type="text" @click="onEdit(row.id)">编辑</el-button>
          <el-button type="text" @click="onDel(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <AddPerm v-if="showDialog" ref="addPermRef" :visible="showDialog" :title="title" @close="showDialog = false" />
  </div>
</template>

<script>
import { getPermissionList, delPermission, getPermissionDetail } from '@/api/permisson.js'
import { getChildren } from '@/utils'
import AddPerm from './components/add-perm.vue'
export default {
  name: 'PermissionPage',
  components: { AddPerm },
  data() {
    return {
      showDialog: false, // 对话框显示关闭
      list: [],
      title: ''
    }
  },
  created() {
    this.getPermissionList()
  },
  mounted() {},
  methods: {
    async onAdd(data) {
      this.title = '新增权限'
      this.showDialog = true // 开启对话框
      await Promise.resolve() // 让后面变成异步任务
      await this.$nextTick() // 让后面变成异步任务
      Object.assign(this.$refs.addPermRef.formData, data) // 将data的数据存储到子组件去
    },
    async getPermissionList() {
      this.list = getChildren(await getPermissionList(), '0')
    },
    // 删除
    onDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delPermission(id)
        this.getPermissionList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 编辑
    async onEdit(id) {
      this.title = '编辑权限'
      this.showDialog = true
      await this.$nextTick()
      this.$refs.addPermRef.formData = await getPermissionDetail(id)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
